********************************************************************************
* Leipziger, L, Aarslew, L, & M. Christensen
* Replication Code for "Do Group-Based Inequalities Feel More Unjust?"
* POQ, 2026
* This code: Fig 2: Effects of Interpersonal and Intergroup inequality on perceived distributive fairness.
********************************************************************************
********************************************************************************
		
clear all
frame rename default data
** Combining datasets 
use "data/IN_data_clean.dta", clear
append using "data/ZA_data_clean.dta"
append using "data/US_data_clean.dta"

lab define country_lab 1 "India" 2 "\forceindent South Africa" 3 "\forceindent US"
lab values country country_lab
**treatments
gen treat=0 if D==1
replace treat=1 if D==2

lab define treat_lb 0 "\forceindent Intergroup ineq." 1 "\forceindent Intergroup ineq."
lab values treat treat_lb

** Marginal effects (all)
qui reg fair_std i.treat pretreat_fair i.country, rob
mat A = r(table)
est store fairness_all // for tables
estadd scalar ajr2 = e(r2_a)

** Marginal effects (India): 
qui reg fair_std i.treat pretreat_fair if country==1, rob
mat B = r(table)
est store fairness_india // for tables
estadd scalar ajr2 = e(r2_a)


** Marginal effects (South Africa)
qui reg fair_std i.treat pretreat_fair if country==2, rob

mat C = r(table)
est store fairness_za // for tables
estadd scalar ajr2 = e(r2_a)

** Marginal effects (USA)
qui reg fair_std i.treat pretreat_fair if country==3, rob
mat D = r(table)
est store fairness_us // for tables
estadd scalar ajr2 = e(r2_a)

*making table SM3
esttab fairness_all fairness_india fairness_za fairness_us using "output/tableSM3.tex", lab nogap wide cells("b(fmt(3))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) tex star(+ 0.10 * 0.05 ** 0.01 *** 0.001) b(%9.3f) ///
	noobs nonotes nonumbers  ///
	stats(ajr2  N, labels("Adjusted R^2" "N") fmt(3 0)) varlabels(_cons Constant) mtitles("Pooled" "India" ///
	"South Africa" "US") replace
	



***EFFECT ON EFFORT
** Marginal effects (all)
qui reg effort_std i.treat pretreat_effort i.country, rob
mat A_effort = r(table)

est store effort_all // for tables
estadd scalar ajr2 = e(r2_a)

** Marginal effects (India): 
qui reg effort_std i.treat pretreat_effort if country==1, rob
mat B_effort = r(table)

est store effort_in // for tables
estadd scalar ajr2 = e(r2_a)

** Marginal effects (South Africa)
qui reg effort_std i.treat pretreat_effort if country==2, rob
mat C_effort = r(table)

est store effort_za // for tables
estadd scalar ajr2 = e(r2_a)

** Marginal effects (USA)
qui reg effort_std i.treat pretreat_effort if country==3, rob
mat D_effort = r(table)

est store effort_us // for tables
estadd scalar ajr2 = e(r2_a)



*making table A4
esttab effort_all effort_in effort_za effort_us using "output/tableSM4.tex", lab nogap wide tex cells("b(fmt(3))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) b(%9.3f) ///
	noobs nonotes nonumbers  ///
	stats(ajr2  N, labels("Adjusted R^2" "N") fmt(3 0)) varlabels(_cons Constant) mtitles("Pooled" "India" ///
	"South Africa" "US") replace
	
	

* Matrix 
mat E = 1,1,A[1,2],A[5,2],A[6,2]\			/// 
		1,2,B[1,2],B[5,2],B[6,2]\			/// 
		1,3,C[1,2],C[5,2],C[6,2]\			/// 
		1,4,D[1,2],D[5,2],D[6,2]\			///
		2,1,A_effort[1,2],A_effort[5,2],A_effort[6,2]\			/// 
		2,2,B_effort[1,2],B_effort[5,2],B_effort[6,2]\			/// 
		2,3,C_effort[1,2],C_effort[5,2],C_effort[6,2]\			/// 
		2,4,D_effort[1,2],D_effort[5,2],D_effort[6,2]
		
mat list E	


frame create plot1
cwf plot1		
svmat E
rename E1 treatment						// 1 = Intergroup
rename E2 Xpos							// Position on X-axis
rename E3 b								// Parameter estimate (Marginal effect)
rename E4 ll							// lower confidence level
rename E5 ul							// upper confidence level


* Plot Panel A: 
tw ///
	(rspike ll ul Xpos if treatment == 1, lc(black)) ///
	(scatter b Xpos if treatment == 1, msym(O) mc(black) msize(med)) ///
	, yscale(range(-0.5 0.5) extend lc(none)) ytit("Perceived Distributive Fairness (sd)") /// 
	yline(0) ylab(-0.4 -0.2 0 0.2 .4, labsize(small) gstyle(solid) glc(gs15%30) notick) ///
	xscale(range(.5 4.6) extend) xtit("Country") ///
	xlab(1 "Pooled" 2 "India" 3 "South Africa" 4 "United States", labsize(small) gstyle(solid) glc(gs15%30) notick) ///
	legend(off) ///
	plotregion(lcolor(black) lwidth(medium)) ///
	scale(1.3) graphregion(m(small)) ///
	tit("{bf:(A)} Effects on Fairness", pos(11) justification(left) size(med)) ///
	name(dist_fair, replace)
	
* Plot Panel B: 
tw ///
	(rspike ll ul Xpos if treatment == 2, lc(black)) ///
	(scatter b Xpos if treatment == 2, msym(O) mc(black) msize(med)) ///
	, yscale(range(-0.5 0.5) extend lc(none)) ytit("Perceived Role of Efforts (sd)") /// 
	yline(0) ylab(-0.4 -0.2 0 0.2 .4, labsize(small) gstyle(solid) glc(gs15%30) notick) ///
	xscale(range(.5 4.6) extend) xtit("Country") ///
	xlab(1 "Pooled" 2 "India" 3 "South Africa" 4 "United States", labsize(small) gstyle(solid) glc(gs15%30) notick) ///
	legend(off) ///
	plotregion(lcolor(black) lwidth(medium)) ///
	scale(1.3) graphregion(m(small)) ///
	tit("{bf:(B)} Effects on Effort", pos(11) justification(left) size(med)) ///
	name(dist_effort, replace)
	
	
* Combine Panels A and B:
graph combine dist_fair dist_effort, cols(2)
graph display, xsize(14) ysize(6) scale(1.3)
graph export "output/figure2.pdf", as(pdf) replace
	

